package chixing.day05;

public class Book {
    private int bookIsbn;
    private String bookTitle;
    private String bookAuthor;
    private boolean available;


    private Book[] borrowedBooks;
    private int borrowedCount;
    public Book(int bookIsbn, String bookTitle, String bookAuthor, boolean available) {
        this.bookIsbn = bookIsbn;
        this.bookTitle = bookTitle;
        this.bookAuthor = bookAuthor;
        this.available = true; // 新书默认可借
        this.borrowedBooks = null; // 图书实例不需要借阅列表
        this.borrowedCount = 0;
    }

    // 用于管理借阅的构造函数
    public Book() {
        this.borrowedBooks = new Book[3]; // 初始化数组，最多3本书
        this.borrowedCount = 0;
    }

    // 借书方法
    public void borrowBook(Book book) {
        // 检查是否已达到最大借阅数量
        if (borrowedCount >= 3) {
            System.out.println("无法借阅《" + book.getBookTitle() + "》，您已达到最大借阅数量（3本）");
            return;
        }

        if (book.isAvailable()) {
            book.setAvailable(false);
            borrowedBooks[borrowedCount] = book;
            borrowedCount++;
            System.out.println("成功借阅《" + book.getBookTitle() + "》");
        } else {
            System.out.println("无法借阅《" + book.getBookTitle() + "》，该书已被借出");
        }
    }

    // 还书方法
    public void returnBook(int isbn) {
        for (int i = 0; i < borrowedCount; i++) {
            if (borrowedBooks[i].getBookIsbn() == isbn) {
                borrowedBooks[i].setAvailable(true);

                for (int j = i; j < borrowedCount - 1; j++) {
                    borrowedBooks[j] = borrowedBooks[j + 1];
                }
                borrowedBooks[borrowedCount - 1] = null;
                borrowedCount--;

                System.out.println("成功归还ISBN为" + isbn + "的书籍");
                return;
            }
        }
        System.out.println("未找到ISBN为" + isbn + "的借阅书籍");
    }

    // 列出当前借阅的所有书籍
    public void listBorrowedBooks() {
        if (borrowedCount == 0) {
            System.out.println("当前没有借阅任何书籍");
            return;
        }

        System.out.println("\n当前借阅的书籍有：");
        for (int i = 0; i < borrowedCount; i++) {
            Book book = borrowedBooks[i];
            System.out.println((i + 1) + ". " + book.getBookTitle() +
                    "（ISBN: " + book.getBookIsbn() +
                    "，作者: " + book.getBookAuthor() + "）");
        }
        System.out.println();
    }
    public int getBookIsbn() {
        return bookIsbn;
    }

    public void setBookIsbn(int bookIsbn) {
        this.bookIsbn = bookIsbn;
    }

    public String getBookTitle() {
        return bookTitle;
    }

    public void setBookTitle(String bookTitle) {
        this.bookTitle = bookTitle;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }
    @Override
    public String toString() {
        return "Book{" +
                "bookIsbn=" + bookIsbn +
                ", bookName='" + bookTitle + '\'' +
                ", bookAuthor='" + bookAuthor + '\'' +
                ", isAvailable=" + available +
                '}';
    }
}
